/** 
 *@Project: okdeer-base-common 
 *@Author: guocp
 *@Date: 2017年5月10日 
 *@Copyright: ©2014-2020 www.okdeer.com Inc. All rights reserved. 
 */
package com.icebartech.core.vo;

import java.io.Serializable;

import com.icebartech.core.enums.CommonResultCodeEnum;
import com.icebartech.core.enums.ResultEnum;
import com.icebartech.core.utils.BeanMapper;

/**
 * ClassName: ResponseBody 
 * @Description: 客户端响应对象
 * @author guocp
 * @date 2017年3月21日
 *
 *	{
 *		data: {
 *			
 *		},
 *		code: Integer,
 *		message: ""
 *	}
 */
public class ResponseData<T> implements Serializable {

	/**
	 * @Fields serialVersionUID : 序列号
	 */
	private static final long serialVersionUID = -4523061141121419191L;

	private T data;

	private String message;

	private Integer code;

	public static <E> ResponseData<E> builder(E data, ResultEnum<Integer> result) {
		return new ResponseData<E>(result, data);
	}
	
	public static <E> ResponseData<E> builderError(E data, String msg) {
		return new ResponseData<E>(CommonResultCodeEnum.FAIL.getCode(), msg, data);
	}

	/**
	 * <p>Title: 构造方法1</p> 
	 * <p>Description:无参 </p>
	 */
	public ResponseData() {
		this(CommonResultCodeEnum.SUCCESS);
	}

	/**
	 * 构造方法2
	 * @param result
	 */
	public ResponseData(ResultEnum<Integer> result) {
		this.code = result.getCode();
		this.message = result.getDesc();
	}

	/**
	 * 构造方法3
	 * @param result
	 * @param data
	 */
	public ResponseData(ResultEnum<Integer> result, T data) {
		this.code = result.getCode();
		this.message = result.getDesc();
		this.data = data;
	}

	/**
	 * 构造方法4
	 * @param result
	 * @param data
	 */
	public ResponseData(Integer code, String message, T data) {
		this.code = code;
		this.message = message;
		this.data = data;
	}

	/**
	 * @Description: ResponseData中Data对象转换
	 * @param destinationClass
	 * @return  
	 * @author guocp
	 * @date 2017年11月8日
	 */
	public <E> ResponseData<E> map(Class<E> destinationClass) {
		ResponseData<E> response = new ResponseData<E>();
		response.setCode(code);
		response.setMessage(message);
		response.setData(BeanMapper.map(this.data, destinationClass));
		return response;
	}
	
	/**
	 * 判断是否成功
	 * @return   
	 * @author guocp
	 * @date 2018年1月8日
	 */
	public boolean isSusscess() {
		return CommonResultCodeEnum.SUCCESS.getCode().equals(this.code);
	}

	public T getData() {
		return data;
	}

	public void setData(T data) {
		this.data = data;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public Integer getCode() {
		return code;
	}

	public void setCode(Integer code) {
		this.code = code;
	}

	/**
	 * ClassName: Builder 
	 * @Description: ResponseData构造器
	 * @author guocp
	 * @date 2017年12月5日
	 *
	 * =================================================================================================
	 *     Task ID			  Date			     Author		      Description
	 * ----------------+----------------+-------------------+-------------------------------------------
	 *
	 */
	public static class Builder<T> {

		private T data;

		private String message;

		private Integer code;

		public Builder<T> code(Integer code) {
			this.code = code;
			return this;
		}

		public Builder<T> message(String message) {
			this.message = message;
			return this;
		}

		public Builder<T> data(T data) {
			this.data = data;
			return this;
		}

		public Builder<T> result(ResultEnum<Integer> result) {
			this.code = result.getCode();
			this.message = result.getDesc();
			return this;
		}

		public ResponseData<T> build() {
			return new ResponseData<T>(this.code, this.message, this.data);
		}
	}
}
